package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysMenuLog;
import com.ysd.lis.mapper.sys.SysMenuLogMapper;
import com.ysd.lis.service.SysMenuLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * cold_chain.t_menu 服务实现类
 * </p>
 *
 * @author Peng
 * @since 2021-10-12
 */
@Service
public class SysMenuLogServiceImpl extends ServiceImpl<SysMenuLogMapper, SysMenuLog> implements SysMenuLogService {

    @Autowired
    private SysMenuLogMapper sysMenuLogMapper;

    @Override
    public Result findSysMenuLogList(String modCode) {
        LambdaQueryWrapper<SysMenuLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenuLog::getDelFlag, 0);
        queryWrapper.eq(SysMenuLog::getStaModCode, modCode);
        queryWrapper.orderByAsc(SysMenuLog::getSeq);
        List<SysMenuLog> menuList = sysMenuLogMapper.selectList(queryWrapper);
        List<SysMenuLog> returnList = new ArrayList<>();
        menuList.forEach(a -> {
            if (StringUtils.isBlank(a.getParentId())) {
                // a.put("children", this.getChildren(a, menuList));
                a.setChildren(this.getChildren(a, menuList));
                returnList.add(a);
            }
        });
        return Result.succ(1, "成功", returnList);
    }

    public List<SysMenuLog> getChildren(SysMenuLog a, List<SysMenuLog> menuList) {
        List<SysMenuLog> result = new ArrayList();
        for (SysMenuLog menu : menuList) {
            if (StringUtils.isBlank(menu.getParentId())) {
                menu.setParentName("");
            }

            if (StringUtils.isNotBlank(menu.getParentId()) && menu.getParentId().equals(a.getId())) {
                menu.setParentName(a.getMenuTitle());
                result.add(menu);
            }

        }
        for (SysMenuLog menu : result) {
            menu.setChildren(getChildren(menu, menuList));
        }
        if (result.size() == 0) {
            return new ArrayList<>();
        }
        return result;

    }
}
